#include "stdafx.h"
#include "GotoTable.h"

namespace compile
{
	// ------------------------------------------ public -------------------------------------

	GotoTable::GotoTable()
	{
	}


	GotoTable::GotoTable(vector<Word> word_vector)
	{
		initIndex(word_vector);
	}

	void GotoTable::initIndex(vector<Word> word_vector)
	{
		for (UNI i = 0; i < word_vector.size(); ++i)
		{
			string key = word_vector.at(i).getKey();
			index_set.insert(key);
		}
	}



	GotoTable::~GotoTable()
	{
		index_set.clear();
		goto_table.clear();
	}


	/**
	* search index
	* @param {map<string, string>} table row(status num) and column(sign)
	* @return word content or empty string
	*/
	string GotoTable::searchTable(pair<string, string> index) const
	{
		auto it = goto_table.find(index);
		if (it == goto_table.end())
		{
			return "";
		}

		return it->second;
	}


	/**
	* print table
	*/
	void GotoTable::printTable() const
	{
		cout << endl << "Goto Table:" << endl;
		for (auto it = goto_table.begin(); it != goto_table.end(); ++it)
		{
			cout << std::get<0>(it->first) << ", " << std::get<1>(it->first)
				<< ":\t" << it->second << endl;
		}
		cout << endl;
	}


	/**
	* get goto table
	*/
	map< pair <string, string>, string> GotoTable::getTable() const
	{
		return goto_table;
	}

	/**
	* add data to goto table
	*/

	void GotoTable::setTable(pair<string, string> index, string content)
	{
		auto it = goto_table.find(index);

		if (it != goto_table.end())
		{
			errorAndExit();
		}

		goto_table.insert(map<pair<string, string>, string>
			::value_type(index, content));
	}


	set<string> GotoTable::getIndexSet() const
	{
		return index_set;
	}


	// ------------------------------------- End public ----------------------------------------
}